import {
	fromJS
} from 'immutable';
import * as constants from './constants';

const defaultState = fromJS({
	topicList: [],
	articleList: [],
	recommendList: [],
	writerList: [],
	articlePage: 1,
	showScroll: false
});

const changeHomeInfo = (state, action) => {
	return state.merge({
		topicList: fromJS(action.topicList),
		articleList: fromJS(action.articleList),
		recommendList: fromJS(action.recommendList),
		writerList: fromJS(action.writerList)
	});
}

const moreList = (state, action) => {
	return state.merge({
		'articleList': state.get('articleList').concat(action.rst),
		'articlePage': fromJS(action.nextPage)
	});
}

const reducer = (state = defaultState, action) => {
	switch (action.type) {
		case constants.CHANGE_HOME_INFO:
			return changeHomeInfo(state, action);
		case constants.MORE_LIST:
			return moreList(state, action);
		case constants.TOGGLE_SCROLL_SHOW:
			return state.set('showScroll', action.show);
		default:
			return state;
	}
}

export default reducer;